package com.jiuli_travel.blog_service.controller;


import com.jiuli_travel.blog_service.model.dto.team.CategorySearchTeamRequest;
import com.jiuli_travel.blog_service.model.dto.team.JoinTeamRequest;
import com.jiuli_travel.blog_service.model.dto.team.TeamEditRequest;
import com.jiuli_travel.blog_service.model.dto.team.TeamInsertRequest;
import com.jiuli_travel.blog_service.model.entity.Team;
import com.jiuli_travel.blog_service.model.vo.TeamVO;
import com.jiuli_travel.blog_service.service.ITeamService;
import com.jiuli_travel.user_service.model.cache.UserCache;
import com.jiuli_travel.user_service.model.enums.UserRoleEnum;
import com.jiuli_travel.user_service.model.vo.UserVO;
import com.jiuli_travel.utils.Result;
import com.jiuli_travel.utils.UserHolder;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.headers.Header;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiuli
 * @since 2024-04-11
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private ITeamService teamService;

    /**
     * 发起团队
     *
     * @param teamInsertRequest 团队传入的参数
     */
    @PostMapping("/publishTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    Result publishTeam(@RequestBody TeamInsertRequest teamInsertRequest){
        return teamService.publishTeam(teamInsertRequest);
    }

    /**
     * 加入团队
     */
    @PostMapping("/joinTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    Result joinTeam(@RequestBody JoinTeamRequest joinTeamRequest){
        return teamService.joinTeam(joinTeamRequest);
    }

    /**
     * 分类标签搜索团队
     * @param searchTeamRequest 分类标签搜索参数
     */
    @GetMapping("/categorySearchTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    void categorySearchTeam(CategorySearchTeamRequest searchTeamRequest){
        teamService.categorySearchTeam(searchTeamRequest);
    }

    /**
     * 搜索框搜索团队
     *
     * @param searchText 搜索文本
     */
    @GetMapping("/searchTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    Result searchTeam(String searchText, Integer size){
        List<TeamVO> teamVOS = teamService.searchTeam(searchText, size);
        return Result.ok(teamVOS);
    }

    /**
     *  解散团队
     * @param teamId 团队id
     */
    @DeleteMapping("/disbandTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    void disbandTeam(Long teamId){
        teamService.disbandTeam(teamId);
    }

    /**
     * 踢出团队
     */
    @DeleteMapping("/kickOutOfTheTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    void kickOutOfTheTeam(Long teamId, Long userId){
        teamService.kickOutOfTheTeam(teamId, userId);
    }

    /**
     * 退出团队
     */
    @DeleteMapping("/leaveTeam")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    void leaveTeam(Long teamId){
        teamService.leaveTeam(teamId);
    }

    /**
     * 队长编辑团队信息
     * @param teamEditRequest
     */
    @PutMapping("editTeamInfo")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    public void editTeamInfo(TeamEditRequest teamEditRequest, HttpServletRequest httpServletRequest){
        // 获取当前用户的id
        Long loginUserId = UserHolder.getUser().getId();
        boolean update = teamService
                .lambdaUpdate()
                .eq(Team::getUserId, loginUserId)
                .update();
    }

    /**
     * 管理员编辑团队信息
     * @param teamEditRequest
     */
    @PutMapping("updateTeamInfo")
    @Parameters({
            @Parameter(name = "token",description = "请求token",required = true,in = ParameterIn.HEADER),
    })
    public void updateTeamInfo(TeamEditRequest teamEditRequest, HttpServletRequest httpServletRequest){
        // 获取当前用户的信息
        UserVO loginUserVO = UserHolder.getUser();
        // 是管理员的话才可以更新数据
        if(!UserRoleEnum.ADMIN.getValue().equals(loginUserVO.getUserRole())) {
            boolean update = teamService
                    .lambdaUpdate()
                    .update();
        }
    }
}
